<?php
namespace app\model;

use project\base\Model;

class UsersModel extends Model
{
  protected $table = 'users';

  /**
   * 检查是否登录   
   *
   * @param array $param where的限制条件
   * 
   * @return $data 查询结果，包括 id 和 用户名
   */
  public function checkLogin(array $param)
  {
    $where = " WHERE ";
    foreach ($param as $key => $value) {
      $where .= $key . " = '" . $value . "' AND ";
    }
    $where = rtrim($where, 'AND ');
    $result = " id , username ";
    // echo($where);
    $data = $this->search($this->table, $where,$result);
    return $data;
  }

  /**
   * 显示有关用户的任务
   *
   * @param string $username 用户名
   * @return $data 返回结果
   */
  public function showInfo($username)
  {
    $result = "b.task_title,b.task_accept,b.task_input,b.task_done,b.id";
    $on = " users a INNER JOIN tasks b ON a.username = b.task_accept OR a.username = b.task_input";
    $where = " WHERE a.username = '$username' ORDER BY b.task_done,b.task_input";
    $data = $this->search($on, $where,$result);
    return $data;
  }

  /**
   * 检测名字是否存在
   *
   * @param string $username 用户名
   * @return $data 返回结果
   */
  public function checkName($username)
  {
    $where = " WHERE username = '$username'";
    $result = " id ";
    $data = $this->search($this->table, $where,$result);
    // var_dump($data);
    return $data;
  }


  public function register($username,$password)
  {
    $col = " username , password ";
    $value = " '$username' , '$password' ";
    $data = $this->add($this->table, $col, $value);
    return $data;
  }


}